package edu.ccut.saturn.studio.menuitem.dialog;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.core.search.SaturnFileSearcher.SearchResult;
import edu.ccut.saturn.studio.core.search.dialog.TowComponentSearchInProjectDialog;
import edu.ccut.saturn.studio.core.search.dialog.TwoCompontentSearchDialog;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;

public class AddMenuItemDialog extends TitleAreaDialog {

	private Text ordertext;
	private Text pathtext;
	private Text labeltext;

	private String label = ""; //$NON-NLS-1$
	private String path = ""; //$NON-NLS-1$
	private String order = ""; //$NON-NLS-1$

	private Shell shell;
	private IProject project;
//	private static final String SEARCH_ELEMENT_ACTION = "action"; //$NON-NLS-1$
//	private static final String SEARCH_ELEMENT_JSP = "jsp"; //$NON-NLS-1$

	/**
	 * Create the dialog
	 * 
	 * @param shell
	 */
	public AddMenuItemDialog(Shell shell, IProject project) {
		super(shell);
		this.shell = shell;
		this.project = project;
	}

	/**
	 * Create contents of the dialog
	 * 
	 * @param parent
	 */

	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		// 设置标题
		setTitle(Messages.getString("AddMenuItemDialog.menutreenode")); //$NON-NLS-1$
		// 设置对话框样式效果
		setMessage(Messages.getString("AddMenuItemDialog.menutreenodemessage")); //$NON-NLS-1$
		return contents;
	}

	@Override
	protected Point getInitialSize() {
		return new Point(497, 337);
	}

	@Override
	protected Control createDialogArea(Composite parent) {

		getShell().setText(
				Messages.getString("AddMenuItemDialog.addmenutreenode")); //$NON-NLS-1$
		Composite container = new Composite(parent, SWT.NONE);
		GridLayout gridlayout = new GridLayout();

		gridlayout.marginHeight = 10;
		gridlayout.marginRight = 10;
		gridlayout.marginLeft = 10;
		gridlayout.marginBottom = 90;
		gridlayout.marginWidth = 3;
		gridlayout.marginTop = 10;
		gridlayout.numColumns = 3;
		container.setLayout(gridlayout);

		final Label label = new Label(container, SWT.NONE);
		label.setLayoutData(new GridData());
		label.setText(Messages.getString("AddMenuItemDialog.showname")); //$NON-NLS-1$

		labeltext = new Text(container, SWT.BORDER);
		labeltext.setTextLimit(50);
		GridData gd_text = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_text.widthHint = 329;
		labeltext.setLayoutData(gd_text);
		labeltext.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});

		final Label pathLabel = new Label(container, SWT.NONE);
		pathLabel.setText(Messages
				.getString("AddMenuItemDialog.actioncomponent")); //$NON-NLS-1$

		pathtext = new Text(container, SWT.BORDER | SWT.READ_ONLY);
		pathtext.setLayoutData(new GridData(318, SWT.DEFAULT));
		pathtext.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});

		final Button button = new Button(container, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				
				String  suffixes [] = {"action","jsp"};
				TowComponentSearchInProjectDialog searchDialog = new TowComponentSearchInProjectDialog(
						shell,suffixes,project);
				if (searchDialog.open() == Window.OK) {
					SearchResult result = (SearchResult)searchDialog.getFirstResult();
					
					String filePath = result.getFile() + "";
					
					if (filePath.endsWith(".jar")) {
						Node RootNode = result.getDocument().getDocumentElement();
						Node InformatonNode =SaturnW3CUtil.getFirstChildByName(RootNode, "information");
						String key = SaturnW3CUtil.getChildNodeValue(InformatonNode, "key");
						pathtext.setText(result.getBundleName() + "::"+ key);
					} else {
						String tempPath = filePath.replace(result.getSrcName()
								+ "", "");
						pathtext.setText(tempPath.replace("\\", "/"));
					}
				}
//				if (project != null) {
//					List<String> oldAllList = SearchFile.searchInSrc(project,
//							new String[] { SEARCH_ELEMENT_JSP,
//									SEARCH_ELEMENT_ACTION }); //$NON-NLS-1$ //$NON-NLS-2$
//					List<String> allList = formatPath(oldAllList);
//					String[] allArray = (String[]) allList
//							.toArray(new String[allList.size()]);
//
//					String[] jspArray = formatArray(allArray,
//							SEARCH_ELEMENT_JSP); //$NON-NLS-1$
//
//					String[] actionArray = formatArray(allArray,
//							SEARCH_ELEMENT_ACTION); //$NON-NLS-1$
//
//					String[][] res = { allArray, jspArray, actionArray };
//
//					FilterDialog dialog = new FilterDialog(shell,
//							new SaturnSWTUtils.SaturnLabelDefaultProvider() {
//								public String getText(Object element) {
//									String laber = (String) element;
//									return laber;
//								}
//							}, res);
//
//					if (dialog.open() == Window.OK) {
//						String ddd = (String) dialog.getFirstResult();
//						pathtext.setText(ddd);
//					}
//				}
			}

		});
		final GridData gd_BrowerButton = new GridData(60, 20);
		button.setLayoutData(gd_BrowerButton);
		button.setText(Messages.getString("AddMenuItemDialog.browse")); //$NON-NLS-1$

		final Label orderLabel = new Label(container, SWT.NONE);
		orderLabel
				.setText(Messages.getString("AddMenuItemDialog.showsequence")); //$NON-NLS-1$

		ordertext = new Text(container, SWT.BORDER | SWT.READ_ONLY);

		GridData gdor_text = new GridData(SWT.FILL, SWT.CENTER, true, false, 2,
				1);
		gdor_text.widthHint = 329;
		ordertext.setLayoutData(gdor_text);
		ordertext.setText(order);

//		dialogChanged();
		return container;
	}

	/**
	 * Create contents of the button bar
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, Messages
				.getString("AddMenuItemDialog.confirm"), false); //$NON-NLS-1$
		createButton(parent, IDialogConstants.CANCEL_ID, Messages
				.getString("AddMenuItemDialog.cancel"), true); //$NON-NLS-1$
		// 新加；
		this.getButton(IDialogConstants.OK_ID).setEnabled(false);
	}

	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			if (dialogChanged() == true) {
				label = labeltext.getText().trim();
				path = pathtext.getText().trim();
				order = ordertext.getText().trim();
			}
		}

		super.buttonPressed(buttonId);
	}

	private boolean dialogChanged() {
		if (SaturnValidation.isEmpty(labeltext.getText())) {
			setErrorMessage(Messages
					.getString("AddMenuItemDialog.shownamelimit")); //$NON-NLS-1$
			this.getButton(IDialogConstants.OK_ID).setEnabled(false);
			return false;

		}

		setErrorMessage(null);
		// 新加；
		this.getButton(IDialogConstants.OK_ID).setEnabled(true);

		return true;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	private List<String> formatPath(List<String> oldList) {
		List<String> newList = new ArrayList<String>();
		for (int i = 0; i < oldList.size(); i++) {
			String str = oldList.get(i);
			str = str.substring(str.indexOf(project.getName()));
			String[] aaa = str.split("\\\\"); //$NON-NLS-1$
			str = "/" + str.substring(str.indexOf(aaa[2])).replace("\\", "/"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			newList.add(str);
		}
		return newList;
	}

	private String[] formatArray(String[] array, String extendName) {
		String[] newArray = new String[array.length];
		for (int i = 0; i < array.length; i++) {
			String str = array[i];
			String extend = str.substring(str.lastIndexOf(".") + 1); //$NON-NLS-1$
			if (extendName.equals(extend)) {
				newArray[i] = str;
			}
		}
		return newArray;
	}
}
